﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MVC.Models;
using MVC.ViewModels;
using System.Web.Security;

namespace MVC.Controllers
{
    public class DiaEscolarController : Controller
    {
        private JardimEntities db = new JardimEntities();

        public void SetDb(JardimEntities novoDb)
        {
            db = novoDb;
        }
                

        [Authorize(Roles = "Funcionario, EE, Administrador")]
        public ViewResult Index(DateTime diaActual, int criancaId)
        {                         

            while (diaActual.DayOfWeek != DayOfWeek.Monday)
            {
                diaActual =diaActual.AddDays(-1);
            }
            ViewBag.Dia = diaActual;
            ViewBag.Crianca = criancaId;

            Crianca temp = db.Criancas.Find(criancaId);
            List<Evento> lista=new List<Evento>();
            List<Evento> lista2;
           
            while (diaActual.DayOfWeek != DayOfWeek.Saturday)
            {
            lista2=
                (from p in db.Eventos.ToList()
                              where (p.DiaEscolar.Dia.ToShortDateString() == diaActual.ToShortDateString())
                              && p.Sala.SalaId== temp.Sala.SalaId
                              select p).ToList();
            diaActual = diaActual.AddDays(1);
            lista.AddRange(lista2);
            }
            Evento[] res = lista.ToArray();
            return View(res);
        }

        [Authorize(Roles = "Funcionario, EE, Administrador")]
        public ActionResult DetalhesEvento(int id, int criancaId)
        {
            Evento evento = db.Eventos.Find(id);
            ViewBag.Crianca = criancaId;

            CriancaEvento autorizacao = (from p in db.CriancaEventos.ToList()
                                    where p.Crianca.CriancaId == criancaId
                                    && p.Evento.EventoId == id
                                    select p).FirstOrDefault();

           ViewBag.Autorizacao= (autorizacao != null);    

            return View(evento);
        }


        [Authorize(Roles = "Funcionario, EE, Administrador")]
        public ActionResult RemoverParticipacao(int id, int criancaId)
        {
            CriancaEvento autorizacao = (from p in db.CriancaEventos.ToList()
                                         where p.Crianca.CriancaId == criancaId
                                         && p.Evento.EventoId == id
                                         select p).FirstOrDefault();

            db.CriancaEventos.Remove(autorizacao);
            db.SaveChanges();
            return RedirectToAction("DetalhesEvento", new { id = id, criancaId = criancaId });
        }

        [Authorize(Roles = "Funcionario, EE, Administrador")]
        public ActionResult AutorizarParticipacao(int id, int criancaId)
        {
            CriancaEvento autorizacao = new CriancaEvento
            { 
                Crianca=db.Criancas.Find(criancaId),
                Evento=db.Eventos.Find(id)
            };

            db.CriancaEventos.Add(autorizacao);
            db.SaveChanges();
            return RedirectToAction("DetalhesEvento", new { id = id, criancaId = criancaId });
        }

          [Authorize(Roles = "Funcionario, EE, Administrador")]
        public ViewResult Ementa(DateTime diaSemana, int criancaId)
        {
            ViewBag.Crianca = criancaId;

            DateTime temp = diaSemana;

            DiaEscolar[] lista1 = (from p in db.DiaEscolars.ToList()
                                   where p.Dia > diaSemana.AddDays(-1)
                                   && p.Dia< (diaSemana.AddDays(5))
                                   select p).ToArray();

            ListaPratos[] lista2 = (from p in db.ListaPratos.ToList()
                                    from a in lista1
                                    where p.Ementa.EmentaId == a.Ementa.EmentaId
                                    select p).ToArray();

            var viewModel = new EmentaViewModel { DiasEscolares = lista1, ListaPratos = lista2 };


            return View(viewModel);
        }

         







          [Authorize(Roles = "Funcionario, EE, Administrador")]
          public ActionResult EscolherPratos(int diaEscolar, int ementa, int criancaId)
          {
              ViewBag.Crianca = criancaId;

              EscolhaPrato escolha = (from p in db.EscolhaPratos.ToList()
                             where p.Crianca.CriancaId == criancaId
                             && p.DiaEscolar.DiaEscolarId == diaEscolar
                             select p).FirstOrDefault();

              if (escolha != null)
              {
                  db.EscolhaPratos.Remove(escolha);
                  db.SaveChanges();
              }




              ViewBag.Nome = db.Criancas.Find(criancaId).Nome;

              ListaPratos[] lista = (from f in db.ListaPratos.ToList()
                                     where f.Ementa.EmentaId == ementa
                                     select f).ToArray();

              List<string> Pratos = (from p in db.Pratos.ToList()
                                     from a in lista
                                     where p.PratoId == a.Prato.PratoId
                                     select p.NomePrato).ToList();
             
                                                   
                
              //Select all the items from the list and place them into a SelectList object.
              //Store the list in ViewData.

              ViewData["Pratos"] = new SelectList(from items in Pratos select items);
              
              ViewData["Dia"] = diaEscolar;

              ViewBag.Dia = (from p in db.DiaEscolars.ToList()
                             where p.DiaEscolarId == diaEscolar
                             select p).FirstOrDefault().Dia;             
              

              return View();
          }

         [Authorize(Roles = "Funcionario, EE, Administrador")]
          [HttpPost]
          public ActionResult EscolherPratos(EscolhaPrato escolha, int criancaId)
          {

              int diaEscolar = Int32.Parse(Request["diaEscolar"]);              

              DateTime dia = (from p in db.DiaEscolars.ToList()
                              where p.DiaEscolarId == diaEscolar
                              select p).FirstOrDefault().Dia;

              EscolhaPrato novaEscolha = new EscolhaPrato
              {
                  Crianca = db.Criancas.Find(criancaId),
                  DiaEscolar = (from p in db.DiaEscolars.ToList()
                                where p.DiaEscolarId==diaEscolar
                                    select p).FirstOrDefault(),                 
                  Prato = (from p in db.Pratos.ToList()
                           where p.NomePrato == escolha.Prato.NomePrato
                           select p).FirstOrDefault(),
              };                        
                  db.EscolhaPratos.Add(novaEscolha);
                  db.SaveChanges();


                  while (dia.DayOfWeek != DayOfWeek.Monday)
                  {
                      dia = dia.AddDays(-1);
                  }

                  return RedirectToAction("Ementa", new { diaSemana = dia , criancaId=criancaId});
         
          }

    }
}